from time import sleep

from selenium import  webdriver
from selenium.webdriver.support.wait import WebDriverWait  # 显示等待
from selenium.webdriver.support import  expected_conditions
from selenium.webdriver.common.by import By

class Test_ran_zhi():
    def setup_method(self):
        driver = webdriver.Chrome()
        driver.get("http://192.168.1.138/ranzhi/www/sys/index.html")
    # def driver_chrome(self):
    #     self.driver = webdriver.Chrome()
    #
    # def get_url(self):
    #     self.driver.get("http://192.168.1.138/ranzhi/www/sys/index.html")
    #     self.driver.maximize_window()

    def test_driver_login(self):
        driver = webdriver.Chrome()
        driver.get("http://192.168.1.138/ranzhi/www/sys/index.html")
        driver.maximize_window()
        driver.find_element(By.XPATH, '//*[@id="account"]').send_keys("admin")
        driver.find_element(By.XPATH, '//*[@id="password"]').send_keys("123456")
        driver.find_element(By.XPATH, '//*[@id="submit"]').click()
        sleep(3)
        driver.find_element(By.XPATH, '//*[@id="s-menu-superadmin"]/button/i').click()
        driver.switch_to.frame("iframe-superadmin")  # 进入框架iframe-superadmin
        WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.XPATH, '//*[@id="shortcutBox"]/div/div[1]/div/a')))
        driver.find_element(By.XPATH, '//*[@id="shortcutBox"]/div/div[1]/div/a').click()
        return driver
    def test_username_fail1(self):
       # self.test_driver_login()
        self.driver = self.test_driver_login()

        WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "account")))
        self.driver.find_element(By.ID, "account").send_keys("")
        sleep(3)
        self.driver.find_element(By.XPATH, '//*[@id="submit"]').click()
        sleep(3)
        text = self.driver.find_element(By.XPATH, '//*[@id="accountLabel"]').text
        assert text == "用户名不能为空。;用户名应当为字母或数字的组合，至少三位", "输入空的用户名进行保存,用例执行失败"
        self.driver.quit()

    def test_username_fail2(self):
        self.driver = self.test_driver_login()

        WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "account")))
        self.driver.find_element(By.ID, "account").send_keys("2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222")
        sleep(3)
        self.driver.find_element(By.XPATH, '//*[@id="submit"]').click()
        sleep(3)
        text = self.driver.find_element(By.XPATH, '//*[@id="accountLabel"]').text
        assert text == "用户名长度应当不超过30，且不小于0。", "输入2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222用户名进行保存,用例执行失败"
        self.driver.quit()

    #
    #
    #
    def test_username_fail3(self):
        self.driver = self.test_driver_login()

        WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "account")))
        self.driver.find_element(By.ID, "account").send_keys("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
        sleep(3)
        self.driver.find_element(By.XPATH, '//*[@id="submit"]').click()
        sleep(3)
        text = self.driver.find_element(By.XPATH, '//*[@id="accountLabel"]').text
        assert text == "用户名长度应当不超过30，且不小于0。", "输入aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa用户名进行保存,用例执行失败"
        self.driver.quit()

    def test_username_fail4(self):
        self.driver = self.test_driver_login()


        WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "account")))
        self.driver.find_element(By.ID, "account").send_keys("22")
        sleep(3)
        self.driver.find_element(By.XPATH, '//*[@id="submit"]').click()
        sleep(3)
        text = self.driver.find_element(By.XPATH, '//*[@id="accountLabel"]').text
        assert text == "用户名应当为字母或数字的组合，至少三位", "输入22进行保存,用例执行失败"
        self.driver.quit()

    def test_username_fail5(self):
        self.driver = self.test_driver_login()

        WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "account")))
        self.driver.find_element(By.ID, "account").send_keys("222")
        sleep(3)
        self.driver.find_element(By.XPATH, '//*[@id="submit"]').click()
        sleep(3)
        # text = self.driver.find_element(By.XPATH, '//*[@id="accountLabel"]').text
        # assert text == "用户名应当为字母或数字的组合，至少三位", "输入空的用户名进行保存,用例执行失败"
        self.driver.quit()

    def test_username_fail6(self):
        self.driver = self.test_driver_login()


        WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "account")))
        self.driver.find_element(By.ID, "account").send_keys("aaa")
        sleep(3)
        self.driver.find_element(By.XPATH, '//*[@id="submit"]').click()
        sleep(3)
        # text = driver.find_element(By.XPATH, '//*[@id="accountLabel"]').text
        # assert text == "用户名应当为字母或数字的组合，至少三位", "输入空的用户名进行保存,用例执行失败"
        self.driver.quit()

    def test_username_fail7(self):
        self.driver = self.test_driver_login()

        WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "account")))
        self.driver.find_element(By.ID, "account").send_keys("aaa")
        self.driver.find_element(By.ID, "realname").send_keys("")
        sleep(3)
        self.driver.find_element(By.XPATH, '//*[@id="submit"]').click()
        sleep(3)

        text = self.driver.find_element(By.XPATH, '//*[@id="realnameLabel"]').text
        assert text == "真实姓名不能为空。", "真实姓名为空测试失败"
        self.driver.quit()




    #
    #
    #
    #
    #
    #
    #
    #
    #
# if __name__ == '__main__':

